﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Mvc.RazorTools.Glyphicons
{
	/// <summary>
	/// Provides a set of extension methods for a Mvc.RazorTools.Glyphicons.Glyphicon
	/// object to be used within the Razor view.
	/// </summary>
	public static class GlyphiconExtensions
	{
		internal static class Constants
		{
			public const string Space = " ";
		}

		/// <summary>
		/// Customizes an existing Mvc.RazorTools.Glyphicons.Glyphicon by adding
		/// class attributes and returning the object. This method can be chained to
		/// apply multiple styles.
		/// </summary>
		/// <param name="icon">The existing Mvc.RazorTools.Glyphicons.Glyphicon to modify.</param>
		/// <param name="items">The class attributes to be added to the existing instance.</param>
		/// <returns>A new modified instance of the Mvc.RazorTools.Glyphicons.Glyphicon object.</returns>
		public static Glyphicon Customize(this Glyphicon icon, params string[] items)
		{
			if (icon == null) throw new ArgumentNullException("icon");
			if (items == null) throw new ArgumentNullException("items");

			Glyphicon returnValue = icon.Clone() as Glyphicon;

			// ***
			// *** Make a copy of each class descriptor
			// ***
			foreach (var item in items)
			{
				returnValue.UpdateClassAttribute(item);
			}

			return returnValue;
		}

		/// <summary>
		/// Applies the FontAwesomeStyles.Large1x style to the specified icon. This method can be chained to
		/// apply multiple styles.
		/// </summary>
		/// <param name="icon">The existing Mvc.RazorTools.Glyphicons.Glyphicon to modify.</param>
		/// <returns>A new modified instance of the Mvc.RazorTools.Glyphicons.Glyphicon object.</returns>
		public static Glyphicon Large(this Glyphicon icon)
		{
			if (icon == null) throw new ArgumentNullException("icon");
			return icon.Customize(GlyphiconsStyles.Large);
		}

		/// <summary>
		/// Applies the FontAwesomeStyles.Large2x style to the specified icon. This method can be chained to
		/// apply multiple styles.
		/// </summary>
		/// <param name="icon">The existing Mvc.RazorTools.Glyphicons.Glyphicon to modify.</param>
		/// <returns>A new modified instance of the Mvc.RazorTools.Glyphicons.Glyphicon object.</returns>
		public static Glyphicon White(this Glyphicon icon)
		{
			if (icon == null) throw new ArgumentNullException("icon");
			return icon.Customize(GlyphiconsStyles.White);
		}

		/// <summary>
		/// Gets the class attribute value only for the given Glyphicon instance.
		/// </summary>
		/// <param name="icon">The existing Mvc.RazorTools.Glyphicons.Glyphicon to modify.</param>
		/// <returns>A System.String value containing the class attribute value from the HTML markup generated for this icon.</returns>
		public static string CssOnly(this Glyphicon icon)
		{
			return icon.ClassAttributes.Values.Aggregate(string.Empty, (o, i) => o + Constants.Space + i).TrimStart();
		}
	}
}
